﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class ServicesSheet_StatUseCarFeeByEmp : System.Web.UI.Page
{
    private Srvtools.WebDataSet WMaster;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DSelYearMonth1.SelectYear = DateTime.Now.Year.ToString();
            DSelYearMonth1.SelectMonth = DateTime.Now.Month.ToString();
            SetDataSource();


            MasterPage_MasterPage master = (MasterPage_MasterPage)this.Page.Master;
            master.HelpHref = "~/Help/ServicesSheet/按人员统计车辆费用.htm";
            master.PageName = "按人员统计车辆费用";
            master.PicUrl = "";
            master.IntiBar();
        }

    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        SetDataSource();
    }

    private void SetDataSource()
    {
        string Sql = "";
        string strYear = "";
        string strMonth = "";

        strYear = DSelYearMonth1.SelectYear;
        strMonth = DSelYearMonth1.SelectMonth;
        if (strYear == "0" || strYear == string.Empty) strYear = "";
        if (strYear.Length == 0)
        {
            strMonth = "";
        }
        else
        {
            if (strMonth == "0" || strMonth == string.Empty) strMonth = "";
        }

        Sql = Sql + "  SELECT Services_SV_StatEmp.EmployeeId, Employees.EmpName,";

        Sql = Sql + "  (SELECT SUM(Services_UseCars.UseKil) ";
        Sql = Sql + "  FROM Services_UseCars";
        Sql = Sql + "  WHERE (Services_UseCars.UserId = Services_SV_StatEmp.EmployeeId) ";
        if (strYear.Length > 0) Sql = Sql + string.Format(" AND (YEAR(Services_UseCars.UCDate) = {0}) ", strYear);
        if (strMonth.Length > 0) Sql = Sql + string.Format(" AND (MONTH(Services_UseCars.UCDate) = {0})", strMonth);
        Sql = Sql + "  GROUP BY Services_UseCars.UserId) AS AllKill,";

        Sql = Sql + "  (SELECT SUM(Services_UseCars.SupExpenses) ";
        Sql = Sql + "  FROM Services_UseCars";
        Sql = Sql + "  WHERE (Services_UseCars.UserId = Services_SV_StatEmp.EmployeeId) ";
        if (strYear.Length > 0) Sql = Sql + string.Format(" AND (YEAR(Services_UseCars.UCDate) = {0}) ", strYear);
        if (strMonth.Length > 0) Sql = Sql + string.Format(" AND (MONTH(Services_UseCars.UCDate) = {0})", strMonth);
        Sql = Sql + "  GROUP BY Services_UseCars.UserId) AS AllSExpenses,";

        Sql = Sql + "  (SELECT SUM(Services_UseCars.UseKil * Services_SV_Cars.PerKMCost + Services_UseCars.SupExpenses)";
        Sql = Sql + "  FROM Services_UseCars INNER JOIN";
        Sql = Sql + "  Services_SV_Cars ON ";
        Sql = Sql + "  Services_UseCars.CarId = Services_SV_Cars.CarId ";
        Sql = Sql + "  WHERE (Services_UseCars.UserId = Services_SV_StatEmp.EmployeeId)  ";
        if (strYear.Length > 0) Sql = Sql + string.Format("  AND (YEAR(Services_UseCars.UCDate) = {0})  ", strYear);
        if (strMonth.Length > 0) Sql = Sql + string.Format("  AND (MONTH(Services_UseCars.UCDate) = {0}) ", strMonth);
        Sql = Sql + "  GROUP BY Services_UseCars.UserId) AS Amount ";
        Sql = Sql + "  FROM Services_SV_StatEmp INNER JOIN ";
        Sql = Sql + "  Employees ON Services_SV_StatEmp.EmployeeId = Employees.EmployeeID";

        DataSet Tmp;
        InitializeComponent();
        Tmp = WMaster.Execute(Sql);

        wgvMaster.DataSource = Tmp.Tables[0];
        wgvMaster.DataBind();

    }

    private void InitializeComponent()
    {
        System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(ServicesSheet_StatUseCarFeeByEmp));
        this.WMaster = new Srvtools.WebDataSet();
        ((System.ComponentModel.ISupportInitialize)(this.WMaster)).BeginInit();
        // 
        // WMaster
        // 
        this.WMaster.Active = false;
        this.WMaster.AlwaysClose = false;
        this.WMaster.DeleteIncomplete = true;
        this.WMaster.Guid = null;
        this.WMaster.LastKeyValues = null;
        this.WMaster.Locale = new System.Globalization.CultureInfo("zh-CN");
        this.WMaster.PacketRecords = 100;
        this.WMaster.Position = -1;
        this.WMaster.RemoteName = "ServiceSheet.SV_StatEmp";
        this.WMaster.ServerModify = false;
        ((System.ComponentModel.ISupportInitialize)(this.WMaster)).EndInit();

    }
}
